package com.newgrand.annexserver.controller;

import com.alibaba.druid.util.StringUtils;
import com.newgrand.annexcommon.model.FileEntity;
import com.newgrand.annexcommon.model.Result;
import com.newgrand.annexcommon.model.vo.AttachmentrecordVo;
import com.newgrand.annexcommon.utils.ResultUtil;
import com.newgrand.annexserver.beans.Attachmentrecord;
import com.newgrand.annexserver.service.AttachmentrecordService;
import com.newgrand.annexserver.storage.StorageContextHolder;
import com.newgrand.annexserver.storage.StoreStrategy;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author ：minyang
 * @date ：Created in 2019/9/9 6:56
 */
@RestController
@RequestMapping("/file")
public class FileController {

    private final static Logger logger = LoggerFactory.getLogger(FileController.class);

    @Autowired
    AttachmentrecordService attachmentrecordService;

    /**
     * 上传
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Result upload(String asr_table, String asr_code, String fileName, String dbtoken, MultipartFile file) {
        AttachmentrecordVo record = new AttachmentrecordVo();
        record.asr_table = asr_table;
        record.asr_code = asr_code;
        record.fileName = fileName;
        record.dbtoken = dbtoken;
        logger.info("upload {}", record);
        if (StringUtils.isEmpty(record.asr_table) || StringUtils.isEmpty(record.asr_code) || StringUtils.isEmpty(record.fileName)) {
            return ResultUtil.fail("asr_table 或 asr_code 为空 或 fileName 为空");
        }
        Result result = null;
        byte[] bytes = null;
        try {
            bytes = file.getBytes();
        } catch (IOException e) {
            result = ResultUtil.fail(e.toString());
            return result;
        }
        String md5 = DigestUtils.md5Hex(bytes);
        record.sessionid = md5;
        try {
            Integer attachCount = attachmentrecordService.countAttachmentrecord(record.asr_table, record.asr_code, record.dbtoken, md5);
            if (attachCount > 0) {
                return ResultUtil.fail("asr_name has uploaded");
            }
            //二进制存储
            StoreStrategy storeStrategy = new StoreStrategy();
            FileEntity fileEntity = new FileEntity();
            fileEntity.setData(bytes);
            fileEntity.setChunk(1);
            fileEntity.setChunks(1);
            String fileId = storeStrategy.save(fileEntity);
            if (!StringUtils.isEmpty(fileId)) {
                Attachmentrecord attachmentrecord = new Attachmentrecord();
                attachmentrecord.setAsrFid(fileId);
                attachmentrecord.setAsrTable(record.asr_table);
                attachmentrecord.setAsrCode(record.asr_code);
                attachmentrecord.setAsrDb(StorageContextHolder.getDbConfig().getDbtoken());
                attachmentrecord.setSessionid(md5);
                attachmentrecordService.addAttachmentrecord(attachmentrecord);
            }
            result = ResultUtil.success();
        } catch (Exception e) {
            logger.error("upload", e);
            result = ResultUtil.fail(e.toString());
        }
        return result;
    }
}
